﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace RSIS.Editor
{
    public partial class HouseTypeImageForm : Form
    {

        public string m_strHouseTypeID;
        public FormType m_FormType;
        private char[] delimiter ={ ' ' };

        public HouseTypeImageForm()
        {
            InitializeComponent();
        }

        public void HouseTypeImageForm_Load(object sender, EventArgs e)
        {
            this.lvwHouseTypeImage.ListViewItemSorter = null;
            this.lvwHouseTypeImage.Items.Clear();
            if (this.m_FormType == FormType.MOD | this.m_FormType == FormType.VIEW)
            {
                RSIS.DataBase.RealInfo pRealInfo = new RSIS.DataBase.RealInfo();

                //加载现有属性
                Npgsql.NpgsqlConnection pCon = new Npgsql.NpgsqlConnection(pRealInfo.GetADOConStr());
                string strQueryString = "SELECT id,housetypeinfoid,description,addingtime FROM rsis_housetypeimage Where housetypeinfoid='" + m_strHouseTypeID + "'";
                Npgsql.NpgsqlDataAdapter pDataAdp = new Npgsql.NpgsqlDataAdapter();
                pDataAdp.SelectCommand = new Npgsql.NpgsqlCommand(strQueryString, pCon);
                Npgsql.NpgsqlCommandBuilder pComBuilder = new Npgsql.NpgsqlCommandBuilder(pDataAdp);

                System.Data.DataSet pDataSet = new DataSet();
                pDataAdp.Fill(pDataSet, "rsis_housetypeimage");
                //pDataSet.Tables["HOUSE"].PrimaryKey=new DataColumn[] {pDataSet.Tables["HOUSE"].Columns["ID"]} ;
                System.Windows.Forms.ListViewItem pLvwItem;
                foreach (System.Data.DataRow pRow in pDataSet.Tables["rsis_housetypeimage"].Rows)
                {

                    if (pRow["description"] == DBNull.Value)
                    {
                        pLvwItem = this.lvwHouseTypeImage.Items.Add("");
                    }
                    else
                    {
                        pLvwItem = this.lvwHouseTypeImage.Items.Add(pRow["description"].ToString());

                    }


                    pLvwItem.Tag = pRow["ID"].ToString();

                    if (pRow["addingtime"] == DBNull.Value)
                    {
                        pLvwItem = this.lvwHouseTypeImage.Items.Add("");
                    }
                    else
                    {
                        pLvwItem.SubItems.Add(pRow["addingtime"].ToString().Split(delimiter)[0]);
                    }
                    

                }


            }
            if (this.m_FormType == FormType.VIEW)
            {
                this.btnAdd.Visible = false;
                this.btnDel.Visible = false;
                this.btnModify.Visible = false;
                //this.Width=this.Width-50;
            }

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            HouseTypeImageInfoForm frmHouseTypeImageInfo = new HouseTypeImageInfoForm();
            frmHouseTypeImageInfo.m_strHouseTypeID = this.m_strHouseTypeID;
            frmHouseTypeImageInfo.m_FormType = FormType.NEW;
            frmHouseTypeImageInfo.ShowDialog();
            HouseTypeImageForm_Load(null, null);
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (this.lvwHouseTypeImage.SelectedItems.Count == 1)
            {
                HouseTypeImageInfoForm frmHouseTypeImageInfo = new HouseTypeImageInfoForm();
                frmHouseTypeImageInfo.m_strHouseTypeID = this.m_strHouseTypeID;
                frmHouseTypeImageInfo.m_strID = this.lvwHouseTypeImage.SelectedItems[0].Tag.ToString();
                frmHouseTypeImageInfo.m_FormType = FormType.MOD;
                frmHouseTypeImageInfo.ShowDialog();
                HouseTypeImageForm_Load(null, null);
            }
            else
            {
                MessageBox.Show("请先选中一条记录！");
                return;
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (this.lvwHouseTypeImage.SelectedItems.Count == 1)
            {

                if (System.Windows.Forms.MessageBox.Show("是否删除当前户型图片信息？", "户型图片编辑工具", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {

                    RSIS.DataBase.RealInfo pRealInfo = new RSIS.DataBase.RealInfo();

                    //加载现有属性
                    Npgsql.NpgsqlConnection pCon = new Npgsql.NpgsqlConnection(pRealInfo.GetADOConStr());
                    string strQueryString = "Select * from rsis_housetypeimage where id=" + this.lvwHouseTypeImage.SelectedItems[0].Tag.ToString();
                    Npgsql.NpgsqlDataAdapter pDataAdp = new Npgsql.NpgsqlDataAdapter();
                    pDataAdp.SelectCommand = new Npgsql.NpgsqlCommand(strQueryString, pCon);
                    Npgsql.NpgsqlCommandBuilder pComBuilder = new Npgsql.NpgsqlCommandBuilder(pDataAdp);

                    System.Data.DataSet pDataSet = new DataSet();
                    pDataAdp.Fill(pDataSet, "rsis_housetypeimage");
                    //pDataSet.Tables["rsis_housetypeimage"].PrimaryKey = new DataColumn[] { pDataSet.Tables["rsis_housetypeimage"].Columns["ID"] };

                    System.Data.DataRow pRow = pDataSet.Tables["rsis_housetypeimage"].Rows[0];//.Find(this.m_strHouseTypeID);
                    pRow.Delete();
                    this.lvwHouseTypeImage.SelectedItems[0].Remove();
                    pDataAdp.Update(pDataSet, "rsis_housetypeimage");
                }
            }

            else
            {

                MessageBox.Show("请先选中一条记录！");
                return;

            }
        }

        private void btnViewHouseImage_Click(object sender, EventArgs e)
        {
            if (this.lvwHouseTypeImage.SelectedItems.Count == 1)
            {
                RSIS.DataBase.RealInfo pRealInfo = new RSIS.DataBase.RealInfo();

                //加载现有属性
                Npgsql.NpgsqlConnection pADOCon = new Npgsql.NpgsqlConnection(pRealInfo.GetADOConStr());
                string strQueryString = "Select * from rsis_housetypeimage where id=" + this.lvwHouseTypeImage.SelectedItems[0].Tag.ToString();
                Npgsql.NpgsqlDataAdapter pDataAdp = new Npgsql.NpgsqlDataAdapter();
                pDataAdp.SelectCommand = new Npgsql.NpgsqlCommand(strQueryString, pADOCon);
                Npgsql.NpgsqlCommandBuilder pComBuilder = new Npgsql.NpgsqlCommandBuilder(pDataAdp);

                System.Data.DataSet pDataSet = new DataSet();
                pDataAdp.Fill(pDataSet, "rsis_housetypeimage");
                //pDataSet.Tables["rsis_housetypeimage"].PrimaryKey = new DataColumn[] { pDataSet.Tables["rsis_housetypeimage"].Columns["ID"] };

                //System.Data.DataRow pRow=pDataSet.Tables["HOUSETYPEINFO"].Rows.Find(m_strID);
                System.Data.DataRow pRow = pDataSet.Tables["rsis_housetypeimage"].Rows[0];//.Find(this.lvwHouseTypeImage.SelectedItems[0].Tag.ToString());

                //生成显示图片框体
                HouseImageForm frmHouseImageForm = new HouseImageForm();

                frmHouseImageForm.m_strHouseID = this.m_strHouseTypeID;
                frmHouseImageForm.m_strID = this.lvwHouseTypeImage.SelectedItems[0].Tag.ToString();
                if (pRow["housetypeimage"] != DBNull.Value)
                {

                    byte[] Imagebytes = null;
                    Imagebytes = (byte[])pRow["housetypeimage"];
                    MemoryStream ms = new MemoryStream(Imagebytes);
                    Bitmap F = new Bitmap(ms);
                    frmHouseImageForm.pbHouseImage.Image = (Image)F;
                    frmHouseImageForm.Text = pRow["description"].ToString();
                    frmHouseImageForm.ShowDialog();

                }
                else
                {
                    MessageBox.Show("该楼盘无图片！");
                    return;

                }
            }
            else
            {
                MessageBox.Show("请先选中一条记录！");
                return;
            }
        }


    }
}